//
//  calendarVC.m
//  CMAccountBook
//
//  Created by 王开政 on 2018/11/1.
//  Copyright © 2018年 王开政. All rights reserved.
//

#import "calendarVC.h"
#import "CalendarView.h"
#import "accountBookDetailVC.h"
@interface calendarVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *Tab;

@property (nonatomic, strong) CalendarView *calendar;

@property(nonatomic, strong) NSMutableArray *muArray;

@property (nonatomic, retain)UIDatePicker *datePick;

@property (nonatomic, retain) UIView *coverView;


@property (nonatomic, retain) UIView *datePickBGView;

@property (nonatomic, strong) NSString *selectDateString;
@end

@implementation calendarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self navig];
    self.muArray = [[NSMutableArray alloc]init];
    [self.view addSubview:self.Tab];
    
    NSDictionary *dic1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"水电",@"name",@"-120.00",@"money",@"支出",@"type",@"10月20日 周四",@"date",@"水电",@"remark", nil];
    NSDictionary *dic2 = [[NSDictionary alloc]initWithObjectsAndKeys:@"工资",@"name",@"+60000.00",@"money",@"收入",@"type",@"10月20日 周四",@"date",@"工资",@"remark", nil];
    NSDictionary *dic3 = [[NSDictionary alloc]initWithObjectsAndKeys:@"零食",@"name",@"-100.00",@"money",@"支出",@"type",@"10月20日 周四",@"date",@"零食",@"remark", nil];
    NSDictionary *dic4 = [[NSDictionary alloc]initWithObjectsAndKeys:@"房租",@"name",@"-2000.00",@"money",@"支出",@"type",@"10月20日 周四",@"date",@"房租",@"remark", nil];
    self.muArray = [[NSMutableArray alloc]initWithObjects:dic1,dic2,dic3,dic4,dic1,dic2,dic3,dic4, nil];
    
    
    [self.view addSubview:self.coverView];
    // Do any additional setup after loading the view.
}
#pragma mark-自定义导航区
-(void)navig{
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.navigationBarHidden=YES;
    //    navigationView*navView = [[navigationView alloc]initWithtitle:@"累计收益" handler:^(UIButton *sender) {
    __weak typeof(self) weakself = self;
    HomeNavigation*navView = [[HomeNavigation alloc]initWithTag:1 title:@"" handler:^(UIButton *sender) {
        
        [weakself backPress];
    }];
    self.calendar.dateSelectButton.frame = CGRectMake(100*kfit, SafeAreaTopHeight-44, ScreenWidth - 200*kfit, 44);
    [navView addSubview:self.calendar.dateSelectButton];
    [self.calendar.dateSelectButton addTarget:self action:@selector(selectBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:navView];
    
}
-(void)backPress{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}
- (CalendarView *)calendar {
    if (!_calendar) {
        
        _calendar = [[CalendarView alloc] initWithFrame:CGRectMake(10, 0, ScreenWidth - 20, 300)];
        
    }
    return _calendar;
}
-(UIView *)coverView{
    
    if (!_coverView) {
        
        self.coverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight )];
        self.coverView.backgroundColor = [UIColor colorWithRed:127/255.0 green:127/255.0 blue:127/255.0 alpha:0.5];
        
        UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelPress)];
        [self.coverView addGestureRecognizer:tapGesturRecognizer];
        self.coverView.hidden = YES;
        
        
    }
    
    
    return  _coverView;
}
-(UIDatePicker *)datePick{
    if (!_datePick) {
        
        _datePick = [[UIDatePicker alloc]initWithFrame:CGRectMake(0,50, ScreenWidth, 200)];
        
        //挑选显示日期的模式
        
        _datePick.datePickerMode = UIDatePickerModeDate;
        
        //设置语言：中文
        
        _datePick.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
        
        //设置 最大时间
        NSDate *  timeDate=[NSDate date];
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        fmt.dateFormat = @"yyyy-MM";
//        _datePick.maximumDate = timeDate;
        
        //添加事件
        
        [_datePick addTarget:self action:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
    }
    return _datePick;
}
-(UIView *)datePickBGView{
    if (!_datePickBGView) {
        _datePickBGView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight-250, ScreenWidth, 250)];
        _datePickBGView.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_datePickBGView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(15, 15)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _datePickBGView.bounds;
        maskLayer.path = maskPath.CGPath;
        _datePickBGView.layer.mask = maskLayer;
        UIButton *left = [[UIButton alloc]initWithFrame:CGRectMake(20, 0, 50, 40)];
        [left setTitle:@"取消" forState:0];
        [left setTitleColor:[UIColor colorWithHexString:@"#999999"] forState:0];
        [left addTarget:self action:@selector(cancelPress) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *right = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-70, 0, 50, 40)];
        [right setTitle:@"确定" forState:0];
        [right setTitleColor:[UIColor colorWithHexString:@"#00CC9A"] forState:0];
        [right addTarget:self action:@selector(right) forControlEvents:UIControlEventTouchUpInside];
        
        [_datePickBGView addSubview:left];
        [_datePickBGView addSubview:right];
        
        [_datePickBGView addSubview:self.datePick];
    }
    return _datePickBGView;
}
- (void)dateChange:(UIDatePicker *)datePicker {
    
    //    [self right];
    NSLog(@"数值改变");
}
-(void)selectBtnClick{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM"];
    NSLog(@"去= %@",self.selectDateString);
//    self.selectDateString = @"2001-01";
    NSDate *date = [dateFormatter dateFromString:self.selectDateString];//上次设置的日期

    if ([self.selectDateString isEqualToString:@""] || self.selectDateString ==nil) {
        date = [NSDate date];
    }
    // 2.3 将转换后的日期设置给日期选择控件
    
    
    [UIView animateWithDuration:0.5 animations:^{
        //
        self.coverView.hidden = NO;
        [self.coverView addSubview:self.datePickBGView];
        self.datePickBGView.hidden = NO;
        //        [self.datePickBGView addSubview:self.datePick];
        
        //        _datePick.datePickerMode = UIDatePickerModeDate;
        [self.datePick setDate:date animated:YES];
    } completion:^(BOOL finished) {
        
    }];
}
-(void)right{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //设置时间格式
    formatter.dateFormat = @"yyyy-MM";
    self.selectDateString = [formatter  stringFromDate:self.datePick.date];
    self.calendar.selectDate = self.datePick.date;
    [self.calendar selectBtnClick];
   
    
    NSLog(@"%@",self.selectDateString);
    [self cancelPress];
}
-(void)cancelPress{
    [UIView animateWithDuration:0.5 animations:^{
        self.coverView.hidden = YES;
//        [self.pickerBGview removeFromSuperview];
        self.datePickBGView.hidden = YES;
        //        [self.datePickBGView removeFromSuperview];
        //        [self.datePick removeFromSuperview];
    } completion:^(BOOL finished) {
        
        
        
    }];
    
}
-(UITableView *)Tab{
    
    if (!_Tab) {
        _Tab = [[UITableView alloc]initWithFrame:CGRectMake(0,SafeAreaTopHeight, ScreenWidth, ScreenHeight-SafeAreaTopHeight)];
        _Tab.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
        _Tab.delegate = self;
        _Tab.dataSource = self;
        //        self.myTab.scrollEnabled = NO;
        _Tab.showsVerticalScrollIndicator = NO;
        _Tab.tableHeaderView = self.calendar;
        _Tab.tableFooterView.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
        if(@available(iOS 11.0, *)){
            _Tab.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _Tab.estimatedRowHeight = 0;
            _Tab.estimatedSectionHeaderHeight = 0;
            _Tab.estimatedSectionFooterHeight = 0;
            
        }
        
    }
    
    return _Tab;
}
#pragma mark -设置cell横线左顶边
-(void)viewDidLayoutSubviews {
    if ([self.Tab respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.Tab setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.Tab respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.Tab setLayoutMargins:UIEdgeInsetsZero];
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
#pragma  make - UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.muArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        
    }
    //    cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
    cell.imageView.image = [UIImage imageNamed:@"我的摇钱桩购买"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text =self.muArray[indexPath.row][@"name"];
    cell.detailTextLabel.text = self.muArray[indexPath.row][@"money"];
    cell.textLabel.font = [UIFont systemFontOfSize:14*kfit];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:14*kfit];
    cell.textLabel.textColor = [UIColor colorWithHexString:@"#101010"];
    cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"#101010"];
    return cell;
}

#pragma  make - UITableViewDelegate
//返回高度，计算不同高度cell 一定要用，否则会出现偏移
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return 50;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 30)];
    view.backgroundColor = [UIColor colorWithHexString:@"#F6F6F8"];
        UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(21*kfit, 0, ScreenWidth/2, 35)];
        leftLabel.textColor = [UIColor colorWithHexString:@"#B7B7B7"];
        leftLabel.font = [UIFont systemFontOfSize:10*kfit];
        leftLabel.textAlignment = NSTextAlignmentLeft;
        leftLabel.text =@"10月21日 周五";
    
        UILabel *rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2 -21*kfit, 0, ScreenWidth/2, 35)];
        rightLabel.textColor = [UIColor colorWithHexString:@"#B7B7B7"];
        rightLabel.font = [UIFont systemFontOfSize:10*kfit];
        rightLabel.textAlignment = NSTextAlignmentRight;
        rightLabel.text =@"收入：60000.00  支出：220.00";
    
        [view addSubview:leftLabel];
        [view addSubview:rightLabel];
    
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    accountBookDetailVC *VC = [[accountBookDetailVC alloc]init];
    VC.detailDic = self.muArray[indexPath.row];
    self.navigationController.navigationBarHidden=NO;
    [VC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:VC animated:YES];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
